import 'package:flutter/material.dart';
import 'addition_page.dart'; // 加法子界面
import 'subtraction_page.dart'; // 减法子界面
import 'multiplication_page.dart'; // 乘法子界面
import 'division_page.dart'; // 除法子界面
import 'mixed_operations_page.dart'; // 混合运算子界面
import 'parentheses_page.dart'; // 带括号运算子界面
import 'nested_operations_page.dart'; // 嵌套运算子界面

class FractionPracticePage extends StatelessWidget {
  final String grade; // 接收年级信息

  const FractionPracticePage({super.key, required this.grade});

  @override
  Widget build(BuildContext context) {

    // 定义年级与练习内容的映射
    final Map<String, List<Map<String, dynamic>>> gradePracticeMap = {
      '一年级': [
        {'title': '纯加法练习题', 'expression': '1 + 2 + 3 = 6', 'page': const AdditionPage()},
        {'title': '纯减法练习题', 'expression': '10 - 3 - 2 = 5', 'page': const SubtractionPage()},
      ],
      '二年级': [
        {'title': '纯加法练习题', 'expression': '1 + 2 + 3 = 6', 'page': const AdditionPage()},
        {'title': '纯减法练习题', 'expression': '10 - 3 - 2 = 5', 'page': const SubtractionPage()},
        {'title': '纯乘法练习题', 'expression': '2 × 3 × 4 = 24', 'page': const MultiplicationPage()},
        {'title': '纯除法练习题', 'expression': '100 ÷ 5 = 20', 'page': const DivisionPage()},
      ],
      '三年级': [
        {'title': '纯加法练习题', 'expression': '1 + 2 + 3 = 6', 'page': const AdditionPage()},
        {'title': '纯减法练习题', 'expression': '10 - 3 - 2 = 5', 'page': const SubtractionPage()},
        {'title': '纯乘法练习题', 'expression': '2 × 3 × 4 = 24', 'page': const MultiplicationPage()},
        {'title': '纯除法练习题', 'expression': '100 ÷ 5 = 20', 'page': const DivisionPage()},
        {'title': '混合运算练习题', 'expression': '2 + 3 × 4 - 6 = 8', 'page': const MixedOperationsPage()},
      ],
      '四年级': [
        {'title': '纯加法练习题', 'expression': '1 + 2 + 3 = 6', 'page': const AdditionPage()},
        {'title': '纯减法练习题', 'expression': '10 - 3 - 2 = 5', 'page': const SubtractionPage()},
        {'title': '纯乘法练习题', 'expression': '2 × 3 × 4 = 24', 'page': const MultiplicationPage()},
        {'title': '纯除法练习题', 'expression': '100 ÷ 5 = 20', 'page': const DivisionPage()},
        {'title': '混合运算练习题', 'expression': '2 + 3 × 4 - 6 = 8', 'page': const MixedOperationsPage()},
        {'title': '带括号的练习题', 'expression': '(2 + 3) × 4 = 20', 'page': const ParenthesesPage()},
      ],
      '五年级': [
        {'title': '纯加法练习题', 'expression': '1 + 2 + 3 = 6', 'page': const AdditionPage()},
        {'title': '纯减法练习题', 'expression': '10 - 3 - 2 = 5', 'page': const SubtractionPage()},
        {'title': '纯乘法练习题', 'expression': '2 × 3 × 4 = 24', 'page': const MultiplicationPage()},
        {'title': '纯除法练习题', 'expression': '100 ÷ 5 = 20', 'page': const DivisionPage()},
        {'title': '混合运算练习题', 'expression': '2 + 3 × 4 - 6 = 8', 'page': const MixedOperationsPage()},
        {'title': '带括号的练习题', 'expression': '(2 + 3) × 4 = 20', 'page': const ParenthesesPage()},
        {'title': '多括号运算练习题', 'expression': '(1 + 2) × (4 - 3) × 5 = 15', 'page': const NestedOperationsPage()},
      ],
      '六年级': [
        {'title': '纯加法练习题', 'expression': '1 + 2 + 3 = 6', 'page': const AdditionPage()},
        {'title': '纯减法练习题', 'expression': '10 - 3 - 2 = 5', 'page': const SubtractionPage()},
        {'title': '纯乘法练习题', 'expression': '2 × 3 × 4 = 24', 'page': const MultiplicationPage()},
        {'title': '纯除法练习题', 'expression': '100 ÷ 5 = 20', 'page': const DivisionPage()},
        {'title': '混合运算练习题', 'expression': '2 + 3 × 4 - 6 = 8', 'page': const MixedOperationsPage()},
        {'title': '带括号的练习题', 'expression': '(2 + 3) × 4 = 20', 'page': const ParenthesesPage()},
        {'title': '多括号运算练习题', 'expression': '(1 + 2) × (4 - 3) × 5 = 15', 'page': const NestedOperationsPage()},
      ],
    };

    // 根据年级获取对应的练习列表
    final practiceList = gradePracticeMap[grade] ?? [];

    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: false,
        title: const Text(
          '练习内容',
          style: TextStyle(color: Colors.white, fontSize: 28),
        ),
        centerTitle: true,
        backgroundColor: const Color(0xFF0D6998),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: ListView(
          children: practiceList.map((practice) {
            return _buildPracticeItem(
              context,
              practice['title'],
              practice['expression'],
              practice['page'],
            );
          }).toList(),
        ),
      ),
    );
  }

  Widget _buildPracticeItem(BuildContext context, String title, String expression, Widget targetPage) {
    return Card(
      color: Colors.blue.shade100,
      margin: const EdgeInsets.symmetric(vertical: 8.0), // 卡片间隔
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12.0), // 圆角
      ),
      elevation: 4.0, // 阴影
      child: ListTile(
        contentPadding: const EdgeInsets.all(16.0), // 内边距
        title: Text(
          title,
          style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
        ),
        subtitle: Text(
          expression,
          style: const TextStyle(fontSize: 16, color: Colors.grey),
        ),
        trailing: ElevatedButton(
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => targetPage),
            );
          },
          style: ElevatedButton.styleFrom(
            padding: const EdgeInsets.all(10),
            backgroundColor: Colors.orange, // 按钮颜色
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(8.0), // 按钮圆角
            ),
          ),
          child: const Icon(Icons.arrow_forward, color: Colors.white), // 箭头图标
        ),
      ),
    );
  }
}
